package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.pojo.Commodity;
import com.pojo.Employee;
import com.pojo.Project;
import com.pojo.Sell;
import com.service.EmployeeService;
import com.service.ProjectService;

@Controller
@RequestMapping("/project")
public class ProjectController {
	@Autowired
	private ProjectService projectService;
	@Autowired
	private EmployeeService employeeService;

	@RequestMapping(value = "/list")
	public String listUsers(Model model) {
		System.out.println("project/list");
		List<Project> projects = projectService.GetAllProject();
		String name;
		for (Project p : projects) {
			name = projectService.GetEmployee(p.getPro_Emp_id()).getEmployee_Name();
			p.setName(name);
			System.out.println(p);
		}
		model.addAttribute("projects", projects);
		return "/jsp/project";
	}

	@RequestMapping(value = "/toproject_add")
	public String toKaoqin_add(Model model) {
		model.addAttribute("project", new Project());
		return "/jsp/project_add";
	}

	@RequestMapping(value = "/project_add")
	public String Kaoqin_add(@ModelAttribute Project project, Model model) {
		String url = "";
		System.out.println(project);
		boolean result1 = true;
		boolean result2 = false;
		// 找编号
		List<Project> pros = projectService.GetAllProject();
		for (Project p : pros) {
			if (p.getProject_Id() == project.getProject_Id())
				result1 = false;
		}
		if (result1) {
			// 找人
			List<Employee> emps = employeeService.GetAllEmployee();
			for (Employee e : emps)
				if (e.getEmployee_Id() == project.getPro_Emp_Id() && project.getName().equals(e.getEmployee_Name())) {
					result2 = true;
				}
		}
		if (result1 && result2) {
			if (projectService.addProject(project))
				url = "redirect:../project/list";
			else {
				String error = "添加失败";
				model.addAttribute("project", project);
				model.addAttribute("error", error);
				url = "jsp/project_add";
			}

		} else {
			String error = "添加失败";
			model.addAttribute("project", project);
			model.addAttribute("error", error);
			url = "jsp/project_add";
		}
		return url;
	}
	
	@RequestMapping(value = "/delete")
	public String delete(@RequestParam Integer pid,Model model) {
		String url="";
		System.out.println("删除"+pid);
		if(projectService.deleteProjectForId(pid))
			url= "redirect:../project/list";
		else {
			String error="删除失败";
			model.addAttribute("error",error);
			url= "redirect:../project/list";
		}
		return url;
	}
	
	@RequestMapping(value = "/toupdate")
	public String toupdate(@RequestParam Integer pid, Model model) {
		model.addAttribute("project", projectService.GetProject(pid));
		return "jsp/project_update";
	}

	@RequestMapping(value = "/update")
	public String update(@ModelAttribute Project project, Model model) {
		String url = "";
		String error = "";
		System.out.println("更新" + project);
		// 找销售员
		boolean result = false;
		List<Employee> emps = employeeService.GetAllEmployee();
		for (Employee e : emps) {
			if (e.getEmployee_Id() == project.getPro_Emp_Id()) {
				result = true;
			}
		}
		if (result) {
			if (projectService.updateProject(project))
				url = "redirect:../project/list";
			else {
				error = "更新失败";
				url = "redirect:../project/list";
			}
		}else {
			error = "员工不存在";
			url = "jsp/project_update";
		}
		model.addAttribute("error", error);
		return url;
	}
}
